//
//  BCOrderResultView.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/20.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCOrderResultView.h"
#import "BCOrderRessultCell.h"
#import "BCShopModel.h"

@interface BCOrderResultView ()<UITableViewDelegate, UITableViewDataSource>
{
    UIView *headView;
}
@property(nonatomic, strong)UITableView *orderResultTable;
@property(nonatomic, strong)NSArray *dataArr;
@end

@implementation BCOrderResultView
-(void)createView
{
    headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor colorWithRed:0.766  green:1  blue:0.831 alpha:1];
    UILabel *headLab = [[UILabel alloc] init];
    UIButton *hidButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [hidButton setImage:[UIImage imageNamed:@"关闭-1"] forState:UIControlStateNormal];
    [hidButton addTarget:self action:@selector(hideHead) forControlEvents:UIControlEventTouchUpInside];
    headLab.text = @"系统已为您选择了最低价";
    headLab.textAlignment = NSTextAlignmentCenter;
    headLab.textColor = [UIColor colorWithRed:0.048  green:0  blue:0 alpha:1];
    headLab.font = [UIFont systemFontOfSize:14];
    
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"叹号"]];
    [headView addSubview:hidButton];
    [headView addSubview:img];
    [headView addSubview:headLab];
    [self addSubview:headView];
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(36);
        make.width.equalTo(self);
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
    }];
    [hidButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.centerY.mas_equalTo(0);
    }];
    [headLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        
    }];
    
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(headLab.mas_left).with.offset(-20);
        
    }];
    
    [self performSelector:@selector(hideHead) withObject:nil afterDelay:4];
    
    
    self.orderResultTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 36, SCREEN_WIDTH, SCREEN_HEIGHT -150) style:UITableViewStylePlain];
    
    [self.orderResultTable registerClass:[BCOrderRessultCell class] forCellReuseIdentifier:@"reuse"];
    self.orderResultTable.delegate = self;
    self.orderResultTable.dataSource = self;
    self.orderResultTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.orderResultTable];
    
}

- (void)hideHead{
    headView.hidden = YES;
    //    Duration动画的运行时间
        [UIView animateWithDuration:.3 animations:^{
    //        动画部分都写在block里
            self.orderResultTable.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *dataArr = [BCOrderViewManager readStuffInCategoryFromOrderWithIndex:indexPath.row];

    return 28*[dataArr count]+65+50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr =  [BCOrderViewManager readCategoryFromOrder];
    return arr.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.Push();
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BCOrderRessultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *arr =  [BCOrderViewManager readCategoryFromOrder];
    cell.style = [arr[indexPath.row][@"category"] integerValue];
    cell.dataArr = [BCOrderViewManager readStuffInCategoryFromOrderWithIndex:indexPath.row];
    return cell;

}

@end
